import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class StatisticsPanel extends JPanel {
    private JTable popularBooksTable;
    private DefaultTableModel popularBooksModel;
    private JTable overdueBooksTable;
    private DefaultTableModel overdueBooksModel;
    private BookDAO bookDAO = new BookDAO();
    private ReaderDAO readerDAO = new ReaderDAO();
    private BorrowDAO borrowDAO = new BorrowDAO();

    public StatisticsPanel() {
        setLayout(new BorderLayout());

        // 创建热门图书表格
        String[] popularColumnNames = {"ID", "书名", "作者", "借阅次数"};
        popularBooksModel = new DefaultTableModel(popularColumnNames, 0);
        popularBooksTable = new JTable(popularBooksModel);
        JScrollPane popularScrollPane = new JScrollPane(popularBooksTable);

        // 创建逾期图书表格
        String[] overdueColumnNames = {"ID", "图书ID", "图书名称", "读者ID", "读者姓名", "借阅日期", "应还日期", "逾期天数"};
        overdueBooksModel = new DefaultTableModel(overdueColumnNames, 0);
        overdueBooksTable = new JTable(overdueBooksModel);
        JScrollPane overdueScrollPane = new JScrollPane(overdueBooksTable);

        // 创建分割面板
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, popularScrollPane, overdueScrollPane);
        splitPane.setDividerLocation(200);

        add(splitPane, BorderLayout.CENTER);

        // 加载数据
        loadPopularBooks();
        loadOverdueBooks();
    }

    // 加载热门图书数据
    private void loadPopularBooks() {
        popularBooksModel.setRowCount(0);

        // 这里应该从数据库获取热门图书数据
        // 为了演示，这里使用模拟数据
        List<Book> books = bookDAO.getAllBooks();

        for (Book book : books) {
            // 模拟借阅次数
            int borrowCount = (int) (Math.random() * 100);

            Object[] row = {
                    book.getId(),
                    book.getTitle(),
                    book.getAuthor(),
                    borrowCount
            };
            popularBooksModel.addRow(row);
        }
    }

    // 加载逾期图书数据
    private void loadOverdueBooks() {
        overdueBooksModel.setRowCount(0);

        List<Borrow> overdueBorrows = borrowDAO.getOverdueBorrows();

        for (Borrow borrow : overdueBorrows) {
            Book book = bookDAO.getBookById(borrow.getBookId());
            Reader reader = readerDAO.getReaderById(borrow.getReaderId());

            if (book != null && reader != null) {
                // 计算逾期天数
                long currentTime = System.currentTimeMillis();
                long dueTime = borrow.getDueDate().getTime();
                long diffDays = (currentTime - dueTime) / (1000 * 60 * 60 * 24);

                Object[] row = {
                        borrow.getId(),
                        book.getId(),
                        book.getTitle(),
                        reader.getId(),
                        reader.getName(),
                        borrow.getBorrowDate(),
                        borrow.getDueDate(),
                        diffDays
                };
                overdueBooksModel.addRow(row);
            }
        }
    }
}